    import {mapState, mapGetters, mapMutations, mapActions} from 'vuex';
	import {quitApp} from '@/utils/page';
	import UserService from '@/services/user/index';
    const service = new UserService();
	
var update = {
	data() {
		return {
			showUpdateDialog: false,
			downloadUrl:'',
			desc:'',
			isForceUpdate:false,//是否强制更新
			clearData:false,//是否清楚本地数据
			updateView:undefined
		}
	},
	onLoad(){
		uni.$on('refuseUpdate',()=>{
			this.refuseUpdate();
		});
		uni.$on('updateNow',()=>{
			this.updateNow();
		})
	},
	onUnload(){
		uni.$off('updateNow');
		uni.$off('refuseUpdate');
	},
	methods: {
		...mapMutations({
		    updateVersionCode:'settings/updateVersionCode',
		}),
		//请求接口，检查是否升级
		checkUpdate() {
			// #ifdef APP-PLUS
			const platform = uni.getSystemInfoSync().platform=='android'?'0':'1';
			let self = this;
			plus.runtime.getProperty(plus.runtime.appid, widgetInfo => {
				const version = widgetInfo.version;
				const name = widgetInfo.name;
				this.updateVersionCode({code:version,name})
				service.versionUpdate({version,platform}).then(versionUpdate => {
					console.log("版本 =",JSON.stringify(versionUpdate));
					//需要更新
					if (versionUpdate.success && versionUpdate.result.needToUpdate == '1') {
						self.downloadUrl = versionUpdate.result.updateUrl;
						self.isForceUpdate = versionUpdate.result.updateType == 1;
						self.desc = versionUpdate.result.desc;
						self.showUpdateDialog = true;
						self.updateView = uni.getSubNVueById('update');
						self.updateView.show('slide-in-bottom',300,function(){
							uni.$emit('needUpdate',{
								updateDesc:self.desc,
								isForceUpdate:self.isForceUpdate
							})
						})
					}
				});
			});
			// #endif
		},
		refuseUpdate() {
			this.showUpdateDialog = false;
			if(this.updateView!=undefined){
				this.updateView.hide('fade-out',300);
			}
			//强制更新，拒绝，直接退出app
			if(this.isForceUpdate){
				quitApp();
			}
		},
		updateNow(){
			uni.removeStorage({
				key:'clearFlag'
			});
			this.showUpdateDialog = false;
			if(this.updateView!=undefined){
				this.updateView.hide('fade-out',300);
			}
			const platform = uni.getSystemInfoSync().platform;
			if (platform == 'android') {
				this.downloadApk();
			} else if (platform == 'ios') {
				plus.runtime.openURL(this.downloadUrl);
			}
		},
		downloadApk() {
			let wgtWaiting = plus.nativeUI.showWaiting("开始下载...", {
				back: 'none'
			});
			let downloadTask = plus.downloader.createDownload(this.downloadUrl, {}, function(download, status) {
				// 下载完成
				wgtWaiting.close();
				if (status == 200) {
					plus.runtime.install(download.filename, {}, {}, function(error) {
						uni.showToast({
							title: '安装失败',
							duration: 1500
						});
					})
				} else {
					uni.showToast({
						title: '更新失败,请检查您的网络',
						duration: 1500
					});
				}
			});
			downloadTask.start();
			downloadTask.addEventListener("statechanged", function(download, status) {
				switch (download.state) {
					case 2:
						wgtWaiting.setTitle("已连接到服务器");
						break;
					case 3:
						const percent = download.downloadedSize / download.totalSize * 100;
						const title = `${parseInt(percent)}%`;
						wgtWaiting.setTitle(title);
						break;
					case 4:
						wgtWaiting.setTitle("下载完成");
						break;
				}
			});
		}
		
	}
}

export default update;
